//
//  UIColor+HexColor.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "UIColor+HexColor.h"

@implementation UIColor (HexColor)

/**
 快速生成RGB颜色
 
 @param r R
 @param g G
 @param b B
 @param alpha 透明度
 @return RGB颜色
 */
+ (UIColor *)gp_colorWithRed:(CGFloat)r green:(CGFloat)g blue:(CGFloat)b alpha:(CGFloat)alpha {
    return [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue: b / 255.0 alpha:alpha];
}


/**
 生成随机颜色
 
 @return 随机颜色
 */
+ (UIColor *)gp_randomColor {
    return [UIColor colorWithRed:arc4random_uniform(256) green:arc4random_uniform(256) blue:arc4random_uniform(256) alpha:1.0f];
}


/**
 Hex转UIColor
 
 @param hexString Hex字符串
 @return UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)hexString {
    return [UIColor colorWithHexString:hexString alpha:1.0];
}


/**
 Hex转UIColor
 
 @param hexString Hex字符串
 @param alphaValue 透明度
 @return UIColor
 */
+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alphaValue
{
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // 判断前缀并剪切掉
    if ([cString hasPrefix:@"0X"])
    cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
    cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
    return [UIColor clearColor];
    
    // 从六位数值中找到RGB对应的位数并转换
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //R、G、B
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:alphaValue];
}

//UIColor转#ffffff格式的字符串
+ (NSString *)hexStringFromColor:(UIColor *)color{
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    
    CGFloat r = components[0];
    CGFloat g = components[1];
    CGFloat b = components[2];
    
    return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
            lroundf(r * 255),
            lroundf(g * 255),
            lroundf(b * 255)];
}

+ (UIColor *)hexColor:(NSString *)hexColor{
    if ([hexColor length] < 6) {
        return nil;
    }
    unsigned int red,green,blue;
    NSRange range;
    range.length = 2;
    range.location = 0;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
    range.location = 2;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
    range.location = 4;
    [[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
    return [UIColor colorWithRed:(float)(red/255.f) green:(float)(green / 255.f) blue:(float)(blue / 255.f) alpha:1.f];
}


/**
 根据十六进制字符串生成RGB颜色（兼容#FFffFF、#FFffFFff、0xFFffFF、0xFFffFFff）
 
 @param hexStr 十六进制颜色字符串
 @return RGB颜色
 */
+ (UIColor *)colorWithHexStr:(NSString *)hexStr {
    // 判断前缀并剪切掉
    if ([hexStr hasPrefix:@"0x"])
    hexStr = [hexStr substringFromIndex:2];
    if ([hexStr hasPrefix:@"#"])
    hexStr = [hexStr substringFromIndex:1];
    
    unsigned int alpha, red, green, blue;
    
    // 6位十六进制颜色
    if (hexStr.length == 6) {
        // alpha
        alpha = 255.0f;
        // R
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(0, 2)]] scanHexInt:&red];
        // G
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(2, 2)]] scanHexInt:&green];
        // B
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(4, 2)]] scanHexInt:&blue];
    }
    // 8位16进制颜色，形如#FFFFFFFF或0xFFFFFFFF，前2位表示透明度，后6位表示颜色
    else if (hexStr.length == 8) {
        // alpha
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(0, 2)]] scanHexInt:&alpha];
        // R
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(2, 2)]] scanHexInt:&red];
        // G
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(4, 2)]] scanHexInt:&green];
        // B
        [[NSScanner scannerWithString:[hexStr substringWithRange:NSMakeRange(6, 2)]] scanHexInt:&blue];
    }
    // 不符合规则（String should be 6 or 8 characters）
    else {
        return [UIColor clearColor];
    }
    
    return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:(float)(alpha/255.0f)];
}


+ (UIColor *)hexAlphaColor:(NSString *)hexColor {
    if ([hexColor length] < 8) {
        return nil;
    }
    
    NSString *hexNewColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
    unsigned int alpha;
    NSRange range = NSMakeRange(6, 2);
    [[NSScanner scannerWithString:[hexNewColor substringWithRange:range]] scanHexInt:&alpha];
    return [[UIColor hexColor:hexNewColor] colorWithAlphaComponent:alpha/255.f];
}



@end
